package org.celery.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.celery.client.vo.GoodsVo;
import org.celery.core.secure.AuthMember;
import org.celery.core.secure.utils.AuthUtil;
import org.celery.modules.goods.entity.Goods;
import org.celery.modules.goods.entity.GoodsCategory;
import org.celery.modules.goods.entity.GoodsPackage;
import org.celery.modules.goods.entity.GoodsStyle;
import org.celery.modules.goods.enums.GoodsStatusEnum;
import org.celery.modules.goods.service.IGoodsCategoryService;
import org.celery.modules.goods.service.IGoodsPackageService;
import org.celery.modules.goods.service.IGoodsService;
import org.celery.modules.goods.service.IGoodsStyleService;
import org.celery.modules.goods.vo.GoodsVO;
import org.celery.modules.goods.wrapper.GoodsPackageWrapper;
import org.celery.modules.goods.wrapper.GoodsStyleWrapper;
import org.celery.modules.goods.wrapper.GoodsWrapper;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.SpringUtil;
import org.springblade.modules.system.entity.Role;
import org.springblade.modules.system.entity.User;
import org.springblade.modules.system.enums.UserStatusEnum;
import org.springblade.modules.system.service.IRoleService;
import org.springblade.modules.system.service.IUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("goods")
@Api(value = "商品")
public class ClientGoodsController {

    private final IGoodsService goodsService;
    private final IGoodsStyleService goodsStyleService;
    private final IGoodsPackageService goodsPackageService;
    private final IGoodsCategoryService goodsCategoryService;

    /**
     * 商品详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情")
    public R<GoodsVo> detail(String goodsId) {
        Goods detail = goodsService.getById(goodsId);

        GoodsVo goodsVo = new GoodsVo();
        if (Func.isNotEmpty(detail)) {
            goodsVo.setGoods(GoodsWrapper.build().entityVO(detail));
            List<GoodsStyle> goodsStyleList = goodsStyleService.list(Wrappers.<GoodsStyle>lambdaQuery()
                    .eq(GoodsStyle::getGoodsId, goodsId)
            );
            goodsVo.setGoodsStyles(GoodsStyleWrapper.build().listVO(goodsStyleList));

            List<GoodsPackage> goodsPackageList = goodsPackageService.list(Wrappers.<GoodsPackage>lambdaQuery()
                    .eq(GoodsPackage::getGoodsId, goodsId)
            );
            goodsVo.setGoodsPackages(GoodsPackageWrapper.build().listVO(goodsPackageList));
        }
        return R.data(goodsVo);
    }

    /**
     * 商品列表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "商品列表")
    public R<IPage<GoodsVO>> list(Goods goods, Query query) {
        IRoleService roleService = SpringUtil.getBean(IRoleService.class);
        List<Role> roles = roleService.list(Wrappers.<Role>lambdaQuery()
                .eq(Role::getRoleAlias, "business")
        );
        if (Func.isEmpty(roles)) {
            return R.data(new Page<>());
        }

        IUserService userService = SpringUtil.getBean(IUserService.class);
        AuthMember authMember = AuthUtil.getMember();
        User user = userService.getById(authMember.getUserId());
        if (Func.isEmpty(user)) {
            return R.data(new Page<>());
        }

        List<User> businessList = userService.list(Wrappers.<User>lambdaQuery()
                .eq(User::getStatus, UserStatusEnum.NORMAL.getStatus())
                .eq(Func.isNotEmpty(user.getArea()), User::getArea, user.getArea())
                .in(User::getRoleId, roles.stream().map(Role::getId).collect(Collectors.toList())
        ));
        if (Func.isEmpty(businessList)) {
            return R.data(new Page<>());
        }


        QueryWrapper<Goods> queryWrapper = Condition.getQueryWrapper(goods);
        if (Func.isNotEmpty(goods.getName())) {
            queryWrapper.lambda().like(Goods::getName, goods.getName());
            goods.setName(null);
        }
        if (Func.isNotEmpty(goods.getSummary())) {
            queryWrapper.lambda().like(Goods::getSummary, goods.getSummary());
            goods.setSummary(null);
        }

        queryWrapper.lambda().eq(Goods::getStatus, GoodsStatusEnum.VERIFIED.getStatus())
                .in(Goods::getCreateUser, businessList.stream().map(User::getId).collect(Collectors.toList()));

        queryWrapper.lambda().orderByAsc(Goods::getSort).orderByDesc(Goods::getCreateTime);
        IPage<Goods> pages = goodsService.page(Condition.getPage(query), queryWrapper);
        return R.data(GoodsWrapper.build().pageVO(pages));
    }

    /**
     * 商品类别列表.
     */
    @GetMapping("/categories")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "商品类别列表")
    public R<List<GoodsCategory>> categories() {
        return R.data(goodsCategoryService.list(Wrappers.<GoodsCategory>lambdaQuery()
                .orderByAsc(GoodsCategory::getSort))
        );
    }


}
